package fx;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

/**
 * Created by thormentor on 19-05-2014.
 */
public class FX {
    public static void popup(Parent parent) {
        Stage stage = new Stage();
        Scene scene = new Scene(parent);
        stage.setScene(scene);
        stage.show();
    }

    public static void close(Parent parent) {
        parent.getScene().getWindow().hide();
    }

    public static ResourceBundle getBundle(Object o){
        Preferences preferences = Preferences.userNodeForPackage(o.getClass());
        String language = preferences.get("language", "pt");
        String country = preferences.get("country", "PT");
        Locale locale = new Locale(language, country);

        String name = o.getClass()
                .getName()
                .replace("Application", "")
                .replace("Controller", "");

        return ResourceBundle.getBundle(name, locale);
    }

    public static void load(Object o) {
        FXMLLoader fxmlLoader = new FXMLLoader(o.getClass().getResource(
                o.getClass().getSimpleName().replace("Controller", "") + ".fxml"), getBundle(o));
        fxmlLoader.setRoot(o);
        fxmlLoader.setController(o);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }
}
